package game.module.mj.dao;

import java.io.File;
import java.nio.charset.Charset;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.io.Files;

import game.module.mj.bean.MjRoomCfg;
import lion.common.Reloadable;

public class MjRoomTemplateCache implements Reloadable  {
    private static Logger logger = LoggerFactory.getLogger(MjRoomTemplateCache.class);
    
    static class SingletonHolder{
    	static MjRoomTemplateCache instance = new MjRoomTemplateCache();
    }
    public static MjRoomTemplateCache getInstance() {
		return SingletonHolder.instance;
	}
    private volatile List<MjRoomCfg> templateList;
	@Override
	public void reload() {
		// TODO Auto-generated method stub
		try{
			String jsonStr = Files.toString(new File("data/mjroom.json"),  Charset.forName("UTF-8"));
			ObjectMapper mapper = new ObjectMapper();
			templateList = mapper.readValue(jsonStr, new TypeReference<List<MjRoomCfg>>() {});
			logger.info("size={}", templateList.size());
		}catch(Exception e){
			logger.error("",e);
		}
	}
	
	public boolean containsConfigIndex(int cfgIndex) {
		return cfgIndex < templateList.size();
	}
	public MjRoomCfg getTemplate(int cfgIndex) {
		return (MjRoomCfg) (templateList.get(cfgIndex));
	}

	public static void main(String[] args) {
		new MjRoomTemplateCache().reload();
	}
}
